package com.polaris.live.dialog

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ScreenUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.TogetherReceiveAdapter2
import com.polaris.live.databinding.DialogTogetherReceive2Binding
import com.polaris.live.resp.back_resp.UserConsumableRewardV2Resp

/**
 * 聚合领取弹窗
 *
 * @author Created by 天晴 on 2024/7/23 15:11
 * @since 1.0.0
 **/
class TogetherReceive2Dialog(
    context: Context,
    val data: List<UserConsumableRewardV2Resp>,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogTogetherReceive2Binding

    private val mAdapter by lazy {
        TogetherReceiveAdapter2()
    }

    override fun getImplLayoutId() = R.layout.dialog_together_receive_2

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogTogetherReceive2Binding.bind(popupImplView)
        val size = if (data.size > 4) 4 else data.size
        mBinding.recycler.apply {
            adapter = mAdapter
            layoutManager = GridLayoutManager(context, size)
            itemAnimator = null
        }
        mAdapter.setList(data)
        mBinding.ok.setOnClickListener {
            dismiss()
        }
        mAdapter.setOnItemClickListener { _, view, position ->
            mAdapter.getItemOrNull(position)?.let {
                if (it.popupText.isNotEmpty() || it.popupTitle.isNotEmpty()) {
                    TogetherDetailsDialog.show(context, view, it)
                }
            }
        }
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()

    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()

    }

    companion object {

        fun show(context: Context, data: List<UserConsumableRewardV2Resp>) {
            XPopup.Builder(context)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .asCustom(TogetherReceive2Dialog(context, data))
                .show()
        }
    }

}